﻿using EMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class CargoTypeProvider : IProvider<CargoType>
    {
        private CargoEntities db = new CargoEntities();
        public int Delete(CargoType t)
        {
            if (t == null) return 0;
            var model = db.CargoType.FirstOrDefault(item => item.Id == t.Id);
            if (model == null) return 0;
            db.CargoType.Remove(model);
            int count = db.SaveChanges();
            return count > 0 ? count : 0;
        }
        public int Insert(CargoType t)
        {
            if (t == null) return 0;
            if (string.IsNullOrEmpty(t.Name)||string.IsNullOrEmpty(t.MemberName)) return 0;  
            if(t.InsertDate == null)return 0;
            db.CargoType.Add(t);
            int count=db.SaveChanges();
            return count > 0 ? count : 0;
        }

        public List<CargoType> Select()
        {
            return db.CargoType.ToList();
        }

        public int Update(CargoType t)
        {

            var model = db.CargoType.FirstOrDefault(item => item.Id == t.Id);
            if (model == null) return 0;
            model.Name = t.Name;
            model.MemberName = t.MemberName;
            model.Tag = t.Tag;
            int count = db.SaveChanges();
            return count > 0 ? count : 0;


        }
    }
}
